#include <windows.h>
#include "resource.h"
#include "resource1.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

char text[400];
char sir1[200],sir2[200],textf1[400],textf2[400];
int ok=0;
int marime=10;
int dim=10;
const char g_szClassName[] = "myWindowClass";
HWND hwndEdit=NULL;
HWND fereastra=NULL,fereastra2=NULL;
int poz_init=0, poz_fin=0;
char sel1[200];

void editare(char sir1[200], char sir2[200], char text[200])
{

int x;
if (dim<=strlen(text))
{
	strcpy(textf1,sir1);
	strcat(textf1,"[");
	strncat(textf1,text,dim);
	strcat(textf1,"]");
	strcat(textf1,sir2);
	strncat(sir1,text,dim);
	strcpy(text,text+dim);
	poz_init=poz_init+dim;
	poz_fin=poz_fin+dim;
	
}
else 
{
	strcpy(textf1,sir1);
	strncat(textf1,"[",1);
	strcat(textf1,text);
	for (int i=0;i<dim-strlen(text);i++)
		strncat(textf1,".",1);
	strncat(textf1,"]",1);
	strcat(textf1,sir2);
	strcat(sir1,text);
	x=strlen(text);
	strcpy(text,text+x);
	dim=dim-x;
	poz_init=poz_init+x;
	poz_fin=poz_fin+x;
}
}

void update(char sir1[200], char sir2[200],int dim)
{
	strcpy(textf1,sir1);
	strcat(textf1,"[");
	for (int i=0;i<dim;i++)
		strcat(textf1,".");
	strcat(textf1,"]");
	strcat(textf1,sir2);
}

void update_siruri(char sir3[200],char sir4[200],int poz)
{
	char aux[200],aux2[200];
	if (poz<strlen(sir1))
	{
		strcpy(aux,sir4);
		strcpy(aux2,sir3);
		strcpy(sir4,sir1+poz);
		strcat(sir4,aux);
		strcpy(sir3,"");
		strncat(sir3,aux2,poz);
	}
	if (poz>strlen(sir3))
	{
		int x=strlen(sir3);
		strcpy(aux,sir3);
		strncat(aux,sir4,poz-x);
		strcpy(sir3,aux);	
		strcpy(sir4,sir4+(poz-x));
	}
}

void stergere(char sir4[400], int poz1, int poz2)
{
	strcpy(sir4,sir4+(poz2-poz1));
	dim=dim+(poz2-poz1);
}


BOOL CALLBACK Setare_buffer(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_INITDIALOG:
			{		
        return TRUE;
			}

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDOK:
                    // When somebody clicks the Add button, first we get the number of
					// they entered
					{
					BOOL bSuccess=TRUE;
					marime = GetDlgItemInt(hwnd, IDC_EDIT1, &bSuccess, FALSE);
					dim=marime;


					if(bSuccess) 
					{
						// Then we get the string they entered
						// First we need to find out how long it is so that we can
						// allocate some memory

						if(marime <=0)
						{
							MessageBox(hwnd, "Numar invalid", "Warning", MB_OK);
						}
					}
					else 
					{
						MessageBox(hwnd, "Couldn't translate that number :(", "Warning", MB_OK);
					}
				EndDialog(hwnd, IDOK);
				}
                break;
               
            }
        break;
        default:
            return FALSE;
    }
    return TRUE;
}





BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_INITDIALOG:
			{		
        return TRUE;
			}

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDOK:
                    // When somebody clicks the Add button, first we get the number of
					// they entered
					{
					BOOL bSuccess=TRUE;
					//int nTimes = GetDlgItemInt(hwnd, IDC_EDIT5, &bSuccess, FALSE);
					if(bSuccess) 
					{
						// Then we get the string they entered
						// First we need to find out how long it is so that we can
						// allocate some memory

						int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDIT2));
						if(len > 0)
						{
							// Now we allocate, and get the string into our buffer

							int i;
							GetDlgItemText(hwnd, IDC_EDIT2, text, len + 1);
							
						}
						else 
						{
							MessageBox(hwnd, "You didn't enter anything!", "Warning", MB_OK);
						}
					}
					else 
					{
					//	MessageBox(hwnd, "Couldn't translate that number :(", "Warning", MB_OK);
					}

				}
                break;
                case IDCANCEL:
                    EndDialog(hwnd, IDCANCEL);
                break;

            }
        break;
		default:
            return FALSE;
	}
    return TRUE;
}


// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
	case WM_CREATE:
		{
		
		}
		break;

	case WM_LBUTTONDOWN:
			{
				
		   }
		break;

	case WM_COMMAND:
		switch(LOWORD(wParam))
			 {
			case ID_OPTIUNI_ADAUGARETEXT:
        {
		
            int ret = DialogBox(GetModuleHandle(NULL), 
                MAKEINTRESOURCE(IDD_DIALOG1), hwnd, AboutDlgProc);
           
			 if(ret == IDCANCEL){
                MessageBox(hwnd, "Terminare editare.", "Notice",
                    MB_OK | MB_ICONINFORMATION);
				while (strlen(text)>0)
				{
					editare(sir1,sir2,text);
					if (strlen(text)) 
					{
						MessageBox(hwnd, "Textul este mai mare decat dimensiunea buffer-ului.Va fi creat un nou buffer.", "Warning",
					 MB_OK | MB_ICONINFORMATION);
						dim=marime;
					}
					
					DestroyWindow(fereastra2); //fereastra cu gap
						fereastra2 = CreateWindowEx(
                                0, // this makes the textbox receive a border
								"Edit",   // predefined class 
                                textf1,         // no window title 
                                WS_HSCROLL | WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER | ES_READONLY,// | WS_EX_NOACTIVATE, 
                                0, 110, 1000,90,   // set size in WM_SIZE message 
                                hwnd,         // parent window 
                                NULL,
                                (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), 
                                NULL); 
					MessageBox(hwnd, "Textul va fi scris in sirul din stanga", "Notice",
					 MB_OK | MB_ICONINFORMATION);
					strcpy(textf2,sir1);
					strcat(textf2,sir2);
					DestroyWindow(hwndEdit);
					hwndEdit = CreateWindowEx(  //fereastra fara gap
                                0, // this makes the textbox receive a border
								"Edit",   // predefined class 
                                textf2,         // no window title 
                                WS_HSCROLL | WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER | ES_READONLY,// | WS_EX_NOACTIVATE, 
                                0, 0, 1000, 90,   // set size in WM_SIZE message 
                                hwnd,         // parent window 
                                NULL,
                                (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), 
                                NULL); 
					if (strlen(text)==0) update(sir1,sir2,dim);
					MessageBox(hwnd, "Verificati buffer-ul pentru a vedea actualizarea", "Notice",
					 MB_OK | MB_ICONINFORMATION);
			
					DestroyWindow(fereastra2);  //fereastra cu gap
						fereastra2 = CreateWindowEx(
                                0, // this makes the textbox receive a border
								"Edit",   // predefined class 
                                textf1,         // no window title 
                                WS_HSCROLL | WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER | ES_READONLY,// | WS_EX_NOACTIVATE, 
                                0, 110, 1000,90,   // set size in WM_SIZE message 
                                hwnd,         // parent window 
                                NULL,
                                (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), 
                                NULL); 
					char aux3[60];
					strcpy(sel1,"Pozitie start selectie: ");
					itoa (poz_init,aux3,10);
					strcat(sel1,aux3);
					strcat(sel1,".Pozitie stop selectie: ");
					char aux4[60];
					itoa (poz_fin,aux4,10);
					strcat(sel1,aux4);
					DestroyWindow(fereastra);
     				fereastra = CreateWindowEx(  //fereastra cursor
                                0, // this makes the textbox receive a border
								"Edit",   // predefined class 
                                sel1,         // no window title 
                                WS_HSCROLL | WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER | ES_READONLY,// | WS_EX_NOACTIVATE, 
                                0, 320, 600, 90,   // set size in WM_SIZE message 
                                hwnd,         // parent window 
                                NULL,
                                (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), 
                                NULL); 
					
					
				}
				goto a;
				}

		
            else if(ret == -1){
                MessageBox(hwnd, "Dialog failed!", "Error",
                    MB_OK | MB_ICONINFORMATION);
			}
		}
		break;

			case ID_OPTIUNI_EXIT:
		{
			DestroyWindow(hwnd);
		}
			case ID_OPTIUNI_CALCULAREAPOZITIEICURSORULUI:
				{

				SendMessage(hwndEdit, EM_GETSEL, (WPARAM) &poz_init, (LPARAM) &poz_fin);
				
				char aux3[60];
					strcpy(sel1,"Pozitie start selectie: ");
					itoa (poz_init,aux3,10);
					strcat(sel1,aux3);
					strcat(sel1,".Pozitie stop selectie: ");
				    char aux4[60];
					itoa (poz_fin,aux4,10);
					strcat(sel1,aux4);
				char sir3[200],sir4[200];
				strcpy(sir3,sir1);
				strcpy(sir4,sir2);
				a:
				update_siruri(sir1,sir2,poz_init);
				strcpy(textf1,sir1);
				strcat(textf1,"[");
				for (int q=0;q<dim;q++)
					strcat(textf1,".");
				strcat(textf1,"]");
				strcat(textf1,sir2);

				DestroyWindow(fereastra2);  //fereastra cu gap
						fereastra2 = CreateWindowEx(
                                0, // this makes the textbox receive a border
								"Edit",   // predefined class 
                                textf1,         // window title 
                                WS_HSCROLL | WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER | ES_READONLY,// | WS_EX_NOACTIVATE, 
                                0, 110, 1000,90,   // set size in WM_SIZE message 
                                hwnd,         // parent window 
                                NULL,
                                (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), 
                                NULL); 
				DestroyWindow(fereastra);
     			fereastra = CreateWindowEx(
                                0, // this makes the textbox receive a border
								"Edit",   // predefined class 
                                sel1,         // no window title 
                                WS_HSCROLL | WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER | ES_READONLY,// | WS_EX_NOACTIVATE, 
                                0, 320, 600, 90,   // set size in WM_SIZE message 
                                hwnd,         // parent window 
                                NULL,
                                (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), 
                                NULL); 
				}
				break;

		break;

			case ID_OPTIUNI_SETAREBUFFER:
				{
				int ret = DialogBox(GetModuleHandle(NULL), 
					 MAKEINTRESOURCE(IDD_DIALOG2), hwnd, Setare_buffer);
				if(ret == IDOK)
				{
                MessageBox(hwnd, "Terminare editare.", "Notice",
                    MB_OK | MB_ICONINFORMATION);
				}
				break;
				}

			case ID_OPTIUNI_STERGERETEXTSELECTAT:
				{
					if (poz_init==poz_fin) 
						MessageBox(hwnd, "Nu ati selectat textul", "Eroare",
						MB_OK | MB_ICONEXCLAMATION);
					else {
						stergere(sir2,poz_init,poz_fin);
						poz_fin=poz_init;
						update(sir1,sir2,dim);
						MessageBox(hwnd, "Verificati buffer-ul pentru a vedea actualizarea", "Notice",
					 MB_OK | MB_ICONINFORMATION);
						DestroyWindow(fereastra2);  //fereastra cu gap
						fereastra2 = CreateWindowEx(
                                0, // this makes the textbox receive a border
								"Edit",   // predefined class 
                                textf1,         // no window title 
                                WS_HSCROLL | WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER | ES_READONLY,// | WS_EX_NOACTIVATE, 
                                0, 110, 1000,90,   // set size in WM_SIZE message 
                                hwnd,         // parent window 
                                NULL,
                                (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), 
                                NULL);
						strcpy(textf2,sir1);
						strcat(textf2,sir2);
						DestroyWindow(hwndEdit);
					hwndEdit = CreateWindowEx(  //fereastra fara gap
                                0, // this makes the textbox receive a border
								"Edit",   // predefined class 
                                textf2,         // no window title 
                                WS_HSCROLL | WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER | ES_READONLY,// | WS_EX_NOACTIVATE, 
                                0, 0, 1000, 90,   // set size in WM_SIZE message 
                                hwnd,         // parent window 
                                NULL,
                                (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), 
                                NULL); 
					char aux3[60];
					strcpy(sel1,"Pozitie start selectie: ");
					itoa (poz_init,aux3,10);
					strcat(sel1,aux3);
					strcat(sel1,".Pozitie stop selectie: ");
				    char aux4[60];
					itoa (poz_fin,aux4,10);
					strcat(sel1,aux4);
					DestroyWindow(fereastra);
     			fereastra = CreateWindowEx(
                                0, // this makes the textbox receive a border
								"Edit",   // predefined class 
                                sel1,         // no window title 
                                WS_HSCROLL | WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER | ES_READONLY,// | WS_EX_NOACTIVATE, 
                                0, 320, 600, 90,   // set size in WM_SIZE message 
                                hwnd,         // parent window 
                                NULL,
                                (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), 
                                NULL); 
					}


				}
				break;
		}break;
		

        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
		
		default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU1);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
	
	if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Gap Buffer",
        WS_OVERLAPPEDWINDOW,
        400, 100, 500, 500,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}